<?php

namespace app\common\business;

use app\common\lib\Arr;
use app\common\model\mysql\ScreenFunding;
use app\common\model\mysql\ScreenIndicators;
use app\common\model\mysql\ScreenInstitution;
use app\common\model\mysql\ScreenProject;
use app\common\model\mysql\ScreenTalent;
use app\common\model\mysql\SystemTitles;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Db;

class Chart
{
    private $talentModel;

    public function __construct()
    {
        $this->talentModel = new ScreenTalent();
    }

    /**
     * 获取最高学历柱图
     * @return array
     * @throws Exception
     */
    public function highEducationBar(): array
    {
        return array_reverse(array_values($this->talentCommonCategoryChart([['education', '<>', 0]],['education'],'学历')));
    }

    /**
     * 获取性别占比饼图
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function genderPie(): array
    {
        $conf = Constant::$Gender;
        unset($conf[0]);
        $talent = $this->talentModel->getChartList([['gender', '<>', 0]], ['gender']);
        $data = [];
        foreach ($conf as $k => $v) {
            $data[$k] = ['name' => $v, 'value' => 0];
        }
        foreach ($talent as $k => $v) {
            if (isset($data[$v['gender']])) {
                $data[$v['gender']]['value']++;
            }
        }
        return array_values($data);
    }

    /**
     * 获取人才指标 各种人才数
     * @return array|array[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function indicatorsNumberPie(): array
    {
        $data = [
            'party' => ['name' => '党政人才', 'value' => 0],
            'enterprise' => ['name' => '企业经营管理人才', 'value' => 0],
            'technology' => ['name' => '专业技术人才', 'value' => 0],
            'high_skill' => ['name' => '高技能人才', 'value' => 0],
            'rural' => ['name' => '农村实用人才', 'value' => 0],
            'social_work' => ['name' => '社会工作人才', 'value' => 0],
        ];
        $model = new ScreenIndicators();
        $area= (new Category)->getCategoryIdByName('酒泉市');
        $indicators = $model->getChartList([['year','=',date('Y')],['area','=',$area]], ['party', 'enterprise', 'technology', 'high_skill', 'rural', 'social_work']);
        if (!empty($indicators)) {
            foreach ($data as $k => $v) {
                $data[$k]['value'] = array_sum(array_column($indicators, $k));
            }
        }
        return array_values($data);
    }



    /**
     * 获取政治面貌饼图
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function politicsPie(): array
    {
        $data=$this->talentCommonCategoryChart([['politics', '<>', 0]],['politics'],'政治面貌');
        $newData=[
            ['name'=>'党员','value'=>0],
            ['name'=>'群众','value'=>0],
            ['name'=>'其他','value'=>0],
        ];
        foreach ($data as $v){
            switch ($v['name']){
                case '中共党员':
                    $newData[0]['value']+=$v['value'];
                    break;
                case '群众':
                    $newData[1]['value']+=$v['value'];
                    break;
                default:
                    $newData[2]['value']+=$v['value'];
            }
        }
        return $newData;
    }

    /**
     * 公用处理方法，统计人才库中category字段
     * @param $where
     * @param $field
     * @param $categoryName
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    private function talentCommonCategoryChart($where,$field,$categoryName): array
    {
        $conf=(new Category())->getCategoryByName($categoryName);
        $talent=$this->talentModel->getChartList($where,$field);
        $data=[];
        foreach ($conf as $v){
            $data[$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        foreach ($talent as $v){
            if(isset($data[$v[$field[0]]])){
                $data[$v[$field[0]]]['value']++;
            }
        }
        return array_values($data);
    }

    /**
     * 获取单位性质柱图
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function unitNatureBar(): array
    {
        return $this->talentCommonCategoryChart([['unit_nature','<>',0]],['unit_nature'],'单位性质');
    }

    /**
     * 查询职称等级统计
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|Exception
     */
    public function titlesLevelBar(): array
    {
        $conf=(new Category())->getCategoryByName('职称等级');
        $newConf=[];
        foreach ($conf as $v){
            $newConf[$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        $titlesModel=new SystemTitles();
        $titlesData=$titlesModel->getList([['status','=',config("status.mysql.table_normal")],['level_id','in',array_column($conf,'id')]],['id','level_id']);
        $newTitles=[];
        foreach ($titlesData as $v){
            $newTitles[$v['id']]=$v;
        }
        $talent=$this->talentModel->getChartList([['titles','<>','']],['id','titles']);
        foreach ($talent as $v){
            $titles=explode(',',$v['titles']);
            $titles=end($titles);
            if(isset($newTitles[$titles])){
                $newConf[$newTitles[$titles]['level_id']]['value']++;
            }
        }
        $newConf=array_values($newConf);
        return [
            ['name'=>'初级职称','value'=>array_sum([$newConf[3]['value'],$newConf[4]['value']])],
            ['name'=>'中级职称','value'=>$newConf[2]['value']],
            ['name'=>'高级职称','value'=>array_sum([$newConf[0]['value'],$newConf[1]['value']])],
        ];
    }





    /**
     * 获取各县市项目扶持资金
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function projectMoneyBar(): array
    {
        $model=new ScreenProject();
        $area=(new Category())->getCategoryByName('区域');
        unset($area[0]);$area=array_values($area);
        $data=[];
        foreach ($area as $v){
            $data[$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        $project=$model->getChartList([],['amount']);
        foreach ($project as $v){
            if(isset($data[$v['area']])){
                $data[$v['area']]['value']+=$v['amount'];
            }
        }
        return array_values($data);
    }

    /**
     * 获取科研机构类别对应机构数量TOP4
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function institutionList(): array
    {
        $model=new ScreenInstitution();
        $category=(new Category())->getCategoryByName('机构类别');
        $newCategory=[];
        foreach ($category as $v){
            $newCategory[$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        $institution=$model->getChartList([],['category']);
        foreach ($institution as $v){
            if(isset($newCategory[$v['category']])){
                $newCategory[$v['category']]['value']++;
            }
        }
        $newCategory=array_values($newCategory);
        $colArr=array_column($newCategory,'value');
        array_multisort($colArr,SORT_DESC,$newCategory);
        return array_slice($newCategory,0,4);
    }



    /**
     *获取各县市引进人才数
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function introductionTalent(): array
    {
        $data=array_values($this->talentCommonCategoryChart([['introduce_area', '<>', 0]],['introduce_area'],'区域'));
        unset($data[0]);
        return array_values($data);
    }

    /**
     * 获取人才年龄占比
     * @return array|array[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function agePie(): array
    {
        $talent=$this->talentModel->getChartList([],['birthday']);
        $data=[
            ['name'=>'25岁以下','value'=>0],
            ['name'=>'25-35岁','value'=>0],
            ['name'=>'36岁以上','value'=>0],
        ];
        foreach ($talent as $v){
            if($v['birthday']!=0&&empty($v['birthday'])){
                continue;
            }
            if($v['birthday']>strtotime('-25 year')){
                $data[0]['value']++;
                continue;
            }
            if($v['birthday']<strtotime('-25 year')&&$v['birthday']>strtotime('-36 year')){
                $data[1]['value']++;
                continue;
            }
            if($v['birthday']<strtotime('-36 year')){
                $data[2]['value']++;
            }
        }
        return $data;
    }

    /**
     * 获取学历占比
     * @return array
     * @throws Exception
     */
    public function educationTower(): array
    {
        $data=$this->highEducationBar();
        foreach ($data as $k=>$v){
            switch ($v['name']){
                case '大专':
                    $data[$k]['name']='专科';
                    break;
                case '研究生':
                    $data[$k]['name']='研究生及以上';
                    break;
                case '本科':
                    break;
                default:
                    unset($data[$k]);
            }
        }
        return array_values($data);
    }




/*--------------------------------------------------------------*/

    /**
     * 获取人才云图页面顶部统计数
     * @return array|array[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function totalCount1(): array
    {
        $data = [
            'total' => ['name' => '人才总量', 'value' => 0],
            'funding' => ['name' => '人才经费投入总量', 'value' => 0],
            'money' => ['name' => '市本级人才发展专项资金', 'value' => 0],
            'introduction' => ['name' => '2023年人才引进总量', 'value' => 0],
            'talent_proportion' => ['name' => '人才占比', 'value' => 0],
        ];
        $model=new ScreenIndicators();
        $indicators=$model->getChartList([['year','=',date('Y')],['area','=',13]],['total','city_money','talent_proportion']);
        if(!empty($indicators)){
            foreach ($data as $k=>$v){
                $data[$k]['value']=array_sum(array_column($indicators,$k));
            }
        }
        $data['funding']['value']=(new ScreenFunding())->where([['year','=',date('Y')],['status','=',1]])->sum('funding');
        $data['introduction']['value']=(new ScreenTalent())->where([['talent_category','=',8],['status','=',1],['introduce_year','like','%2023%']])->count();
        return $data;
    }
    /**
     * 获取各县市人才投入指标
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function indicatorsList(): array
    {
        $model=new ScreenIndicators();
        $category=new Category();
        $area= $category->getCategoryIdByName('酒泉市');
        $categoryData=$category->categoryDictionaries();
        $indicators=$model->getChartList([['year','=','2022'],['area','<>',$area]],['id','area','total','talent_proportion','money']);
        foreach ($indicators as $k=>$v){
            $indicators[$k]['area_name']= $categoryData[$v['area']] ?? '';
            $indicators[$k]['num']=$k+1;
            $indicators[$k]['total']=$v['total'].' 万人';
            $indicators[$k]['talent_proportion']=round($v['talent_proportion'],2).'%';
            $indicators[$k]['money']=round($v['money']).' 万元';
        }
        return $indicators;
    }

    /**
     *重点人才情况
     * @return array|string[]
     * @throws Exception
     */
    public function keyTalents(): array
    {
        $category=Constant::$TalentCategory;
        $data=[];
        foreach ($category as $k=>$v){
            $data[$k]=['id'=>$k,'name'=>$v,'count'=>0];
        }
        $talentData=(new Talent())->getTableList()['list'];
        $sum=0;
        foreach ($talentData as $v){
            if(empty($v['talent_category'])){
               continue;
            }
            foreach ($v['talent_category'] as $item) {
                $data[$item]['count']++;
            }
            $sum++;
        }
        foreach ($data as $k=>$v){
            $data[$k]['percentage']=$sum==0?0:round($v['count']/$sum*100,2);
        }
        $data=Arr::arrSort($data,'count',SORT_DESC);
        return  array_values($data);
    }

    /**
     * 高层次平台建设
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function scientificCategoryCount(): array
    {
        $category=(new Category())->getCategoryByName('机构类别');
        $category=Arr::arrDic($category,'id');
        $scientific=(new ScreenInstitution())->getChartList([],['id','category']);
        foreach ($scientific as $k=>$v){
            if(!$category[$v['category']]){
                continue;
            }
            if(!isset($category[$v['category']]['count'])){
                $category[$v['category']]['count']=0;
            }
            $category[$v['category']]['count']++;
        }
        return array_values($category);
    }

    /**
     * 年龄分布
     * @return array|array[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function ageDistribution(): array
    {
        $talent=$this->talentModel->getChartList([],['birthday']);
        $data=[
            ['name'=>'55岁以上','value'=>10],
            ['name'=>'40-49岁','value'=>20],
            ['name'=>'30-39岁','value'=>30],
            ['name'=>'30岁以下','value'=>40],
        ];
        foreach ($talent as $v){
            if($v['birthday']!=0&&empty($v['birthday'])){
                continue;
            }
            if($v['birthday']<strtotime('-55 year')){
                $data[0]['value']++;
                continue;
            }
            if($v['birthday']<strtotime('-40 year')&&$v['birthday']>strtotime('-50 year')){
                $data[1]['value']++;
                continue;
            }
            if($v['birthday']<strtotime('-30 year')&&$v['birthday']>strtotime('-40 year')){
                $data[2]['value']++;
                continue;
            }
            if($v['birthday']>strtotime('-30 year')){
                $data[3]['value']++;
            }
        }
        return $data;
    }

    /**
     * 争取资金
     * @return mixed
     * @throws Exception
     */
    public function fundingList(){
        $area=(new Category())->getCategoryByName('区域');
        $data=[];
        $model=new ScreenFunding();
        $lastYearData=$model->getChartList([
            ['status','=',1],
            ['year','=',date('Y')-1]
        ]);
        $lastYear=[];
        foreach ($lastYearData as $item) {
            $lastYear[$item['area']]=$item;
        }

        $currentYearData=$model->getChartList([
            ['status','=',1],
            ['year','=',date('Y')]
        ]);
        $currentYear=[];
        foreach ($currentYearData as $item) {
            $currentYear[$item['area']]=$item;
        }
        foreach ($area as $item) {
            $data[$item['id']]=[
                'name'=>$item['name'],
                (date('Y')-1)=>(isset($lastYear[$item['id']])?$lastYear[$item['id']]['funding']:0).'万',
                date('Y')=>(isset($currentYear[$item['id']])?$currentYear[$item['id']]['funding']:0).'万',
                'increase'=>(isset($currentYear[$item['id']])?$currentYear[$item['id']]['increase']:0).'万',
                'increase_percentage'=>(isset($currentYear[$item['id']])?round($currentYear[$item['id']]['increase_percentage'],2):0).'%',
            ];
        }
        return array_values($data);
    }

    /**
     * 各县市项目数量、资金雷达
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function projectInfoRadar(): array
    {
        $model=new ScreenProject();
        $area=(new Category())->getCategoryByName('区域');
        unset($area[0]);$area=array_values($area);
        $areaCategory=[];
        foreach ($area as $v){
            $areaCategory[$v['id']]=['name'=>$v['name'],'project'=>0,'money'=>0];
        }
        $data=['area'=>array_column($area,'name'),'project'=>[],'money'=>[]];
        $project=$model->getChartList([],['area','amount']);
        foreach ($project as $k=>$v){
            if(isset($areaCategory[$v['area']])){
                $areaCategory[$v['area']]['project']++;
                $areaCategory[$v['area']]['money']+=$v['amount'];
            }
        }
        foreach ($areaCategory as $k=>$v){
            $data['project'][]=$v['project'];
            $data['money'][]=$v['money'];
        }
        return $data;
    }

    /**
     * 获取各县市项目数量
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function projectBar(): array
    {
        $model=new ScreenProject();
        $area=(new Category())->getCategoryByName('区域');
        unset($area[0]);$area=array_values($area);
        $data=[];
        foreach ($area as $v){
            $data[$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        $project=$model->getChartList([],['area']);
        foreach ($project as $v){
            if(isset($data[$v['area']])){
                $data[$v['area']]['value']++;
            }
        }
        return array_values($data);
    }

    /**
     * 年度省级人才项目人员情况统计表
     * @param $year
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function talentProject($year): array
    {
        $model=new ScreenProject();
        $data=$model->getChartList([['year','=',$year]]);
        $total=$model->where(['status'=>1,'year'=>$year])->sum('amount');
        foreach ($data as $k=>$v){
            $data[$k]['percentage']=round($v['amount']/$total*100,2);
        }
        return $data;
    }

    /**
     * 人才引进
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function talentIntroduce(): array
    {
        $list=$this->talentModel->getChartList([['talent_category','=',8]]);
        $data=[
            'chart'=>[],
            'total'=>count($list),
        ];
        $now=date('Y');
        $year[$now]=0;
        $year[$now-1]=0;
        $year[$now-2]=0;
        $category=(new Category())->getCategoryByName('区域');
        foreach ($category as $v){
            $data['chart'][$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        foreach ($list as $v){
            if(!empty($year[$v['introduce_year']])){
                $year[$v['introduce_year']]++;
            }
            if(!empty($v['introduce_area'])){
                $data['chart'][$v['introduce_area']]['value']++;
            }
        }
        foreach ($year as $k=>$v){
            $data['year'][]=['year'=>$k,'value'=>$v];
        }
        $data['chart']=array_values($data['chart']);
        return $data;
    }

    /**
     * 市级重点人才项目
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function talentProjectChart(): array
    {
        $nowYear=date('Y');
        $data=[];
        $list=(new ScreenProject())->getChartList([['category','=',9]]);
        for($i=0;$i<5;$i++){
            $data[$nowYear-$i]=['year'=>$nowYear-$i.'年','amount'=>0,'count'=>0];
        }
        foreach ($list as $v){
            if(isset($data[$v['year']])){
                $data[$v['year']]['amount']+=$v['amount'];
                $data[$v['year']]['count']++;
            }
        }
        return array_reverse(array_values($data));
    }

    /**
     * 人才实训基地
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function talentBaseChart(): array
    {
        $nowYear=date('Y');
        $data=[];
        $list=(new ScreenProject())->getChartList([['category','=',126]]);
        for($i=0;$i<5;$i++){
            $data[$nowYear-$i]=['year'=>$nowYear-$i.'年','amount'=>0,'count'=>0];
        }
        foreach ($list as $v){
            if(isset($data[$v['year']])){
                $data[$v['year']]['amount']+=$v['amount'];
                $data[$v['year']]['count']++;
            }
        }
        return array_reverse(array_values($data));
    }

    /**
     * 年度全市人才总量及投入
     * @param $year
     * @return array|array[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function talentTotal($year): array
    {
        $data = [
            'total' => ['name' => '人才总量', 'value' => 0,'change'=>0],
            'talent_proportion' => ['name' => '各类人才占人口比例', 'value' => 0,'change'=>0],
            'money' => ['name' => '人才发展专项资金', 'value' => 0,'change'=>0],
            'city_money' => ['name' => '市本级人才发展专项资金', 'value' => 0,'change'=>0],
        ];
        $model=new ScreenIndicators();
        $now=$model->getChartList([['year','=',date($year)]],['total','talent_proportion','money','city_money']);
        $last=$model->getChartList([['year','=',date($year-1)]],['total','talent_proportion','money','city_money']);
        foreach ($data as $k=>$v){
            if($k!='talent_proportion'){
                $data[$k]['value']=!empty($now)?array_sum(array_column($now,$k)):0;
                $lastValue=!empty($last)?array_sum(array_column($last,$k)):0;
            }else{
                $col=!empty($now)?array_column($now,$k):[0];
                $lastCol=!empty($last)?array_column($last,$k):[0];
                $data[$k]['value']=round(array_sum($col)/count($col),2);
                $lastValue=round(array_sum($lastCol)/count($lastCol),2);
            }
            $change=round(($data[$k]['value']-$lastValue)/($lastValue==0?1:$lastValue),2);
            $data[$k]['change']=($change>=0?'+'.$change:'-'.$change).'%';
            if($k!='talent_proportion'){
                $data[$k]['value']=number_format($data[$k]['value']);
            }
        }
        return $data;
    }

    /**
     * 重点科研机构数据
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function importantScientific(): array
    {
        $category=(new Category())->getCategoryByName('区域');
        $list=(new ScreenInstitution())->getChartList();
        $data=[
            'total'=>count($list),
            'chart'=>[]
        ];
        foreach ($category as $v){
            $data['chart'][$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        foreach ($list as $item) {
            $data['chart'][$item['area']]['value']++;
        }
        $data['chart']=array_values($data['chart']);
        return $data;
    }

    /**
     * 人才技术领域
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function talentTechnology(): array
    {
        $category=(new Category())->getCategoryByName('技术领域');
        $data=[];
        foreach ($category as $k=>$v){
            $data[$v['id']]=['name'=>$v['name'],'value'=>0];
        }
        $list=$this->talentModel->getChartList();
        foreach ($list as $k=>$v){
            if(!empty($v['technology'])){
                $data[$v['technology']]['value']++;
            }
        }
        return array_values($data);
    }

    public function enterpriseTalent(){

    }

    /**
     * @param $pram
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function talentList($pram){
        $where=[];
        if(!empty($pram['talentCategory'])){
            $where[]=['','exp',Db::raw("FIND_IN_SET({$pram['talentCategory']},talent_category)")];
        }
        if(!empty($pram['introduceArea'])){
            $where[]=['introduce_area','=',$pram['introduceArea']];
        }
        $data=$this->talentModel->getTableList($where,empty($pram['page'])?null:$pram['page'],empty($pram['limit'])?null:$pram['limit']);
        $category=(new Category())->categoryDictionaries();
        $talentCategory=Constant::$TalentCategory;
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['gender_text']= Constant::$Gender[$v['gender']] ?? '-';
            $data['list'][$k]['nation_text']= $category[$v['nation']] ?? '-';
            $data['list'][$k]['politics_text']= $category[$v['politics']] ?? '-';
            $data['list'][$k]['technology_text']= $category[$v['technology']] ?? '-';
            $data['list'][$k]['education_text']= $category[$v['education']] ?? '-';
            $data['list'][$k]['degree_text']= $category[$v['degree']] ?? '-';
            $data['list'][$k]['birthday']=!empty($v['birthday'])?date('Y-m-d',$v['birthday']):'';
            $data['list'][$k]['graduation_time']=!empty($v['graduation_time'])?date('Y-m-d',$v['graduation_time']):'';
            $data['list'][$k]['technical_position_time']=!empty($v['technical_position_time'])?date('Y-m-d',$v['technical_position_time']):'';
            $data['list'][$k]['related_profession']=empty($v['related_profession'])?[]:Arr::toNumber(explode(',',$v['related_profession']));
            $data['list'][$k]['talent_category']=empty($v['talent_category'])?[]:Arr::toNumber(explode(',',$v['talent_category']));
            $data['list'][$k]['titles']=empty($v['titles'])?[]:Arr::toNumber(explode(',',$v['titles']));
            $talentCategoryText=[];
            foreach ($data['list'][$k]['talent_category'] as $item) {
                $talentCategoryText[]=$talentCategory[$item];
            }
            $data['list'][$k]['talent_category_text']=implode(',',$talentCategoryText);
        }
        return $data;
    }

    /**
     * @param $pram
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function scientificList($pram){
        $where=[];
        if(!empty($pram['category'])){
            $where[]=['category','=',$pram['category']];
        }
        $data=(new ScreenInstitution())->getTableList($where,empty($pram['page'])?null:$pram['page'],empty($pram['limit'])?null:$pram['limit']);
        $category=(new Category())->categoryDictionaries();
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['category_name']= $category[$v['category']] ?? '-';
            $data['list'][$k]['area_name']= $category[$v['area']] ?? '-';
        }
        return $data;
    }

    public function getMarkers(){
        $dictionary=[
            '620902'=>'14',//肃州区
            '620921'=>'17',//金塔县
            '620922'=>'18',//瓜州县
            '620923'=>'19',//肃北县
            '620982'=>'16',//敦煌市
            '620981'=>'15',//玉门市
            '620924'=>'20',//阿克塞
        ];
        $data=[];
        $model=new ScreenIndicators();
        foreach ($dictionary as $key=>$item) {
            $total=$model->removeOption('where')->where([
                ['year','=','2022'],
                ['status','=',1],
                ['area','=',$item]
            ])->field('total')->find();
            $data[$key]=empty($total)?0:$total['total'];
        }
        return $data;
    }

    public function areaProportion(){
        $area=(new Category())->getCategoryByName('区域');
        $data=[];
        foreach ($area as $item) {
            if($item['id']!=13){
                $data[$item['id']]=['name'=>$item['name'],'value'=>0,'percentage'=>0,'id'=>$item['id']];
            }
        }
        $indicators=(new ScreenIndicators())->getChartList([['area','in',array_keys($data)],['year','=','2022']]);
        foreach ($indicators as $k=>$v){
            $data[$v['area']]['percentage']=$v['talent_proportion']*1;
            $data[$v['area']]['value']=$v['total']*1;
        }
        return array_values($data);
    }
}
